<?php
namespace app\clerk\model;
use app\common\model\Goods as GoodsModel;
use think\Db;

/**
 * 菜品模型
 */
class Goods extends GoodsModel
{
    /**
     * 添加
     */
    public function add(array $data,$shop_id)
    {
        if (!isset($data['images']) || empty($data['images'])) {
            $this->error = '请上传菜品图片';
            return false;
        }
		$data['shop_id'] = $shop_id;
        $data['content'] = isset($data['content']) ? $data['content'] : '';
        $data['wxapp_id'] = $data['spec']['wxapp_id'] = self::$wxapp_id;

        // 开启事务
        Db::startTrans();
        try {
            // 添加菜品
            $this->allowField(true)->save($data);
            // 菜品规格
            $this->addGoodsSpec($data);
            // 菜品图片
            $this->addGoodsImages($data['images']);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
        }
        return false;
    }

    /**
     * 添加菜品图片
     */
    private function addGoodsImages($images)
    {
        $this->image()->delete();
        $data = array_map(function ($image_id) {
            return [
                'image_id' => $image_id,
                'wxapp_id' => self::$wxapp_id
            ];
        }, $images);
        return $this->image()->saveAll($data);
    }

    /**
     * 编辑
     */
    public function edit($data)
    {
        if (!isset($data['images']) || empty($data['images'])) {
            $this->error = '请上传菜品图片';
            return false;
        }
        $data['content'] = isset($data['content']) ? $data['content'] : '';
        $data['wxapp_id'] = $data['spec']['wxapp_id'] = self::$wxapp_id;
        // 开启事务
        Db::startTrans();
        try {
            // 保存菜品
            $this->allowField(true)->save($data);
            // 菜品规格
            $this->addGoodsSpec($data, true);
            // 菜品图片
            $this->addGoodsImages($data['images']);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            $this->error = $e->getMessage();
            return false;
        }
    }

    /**
     * 添加菜品规格
     */
    private function addGoodsSpec(&$data, $isUpdate = false)
    {
        // 更新模式: 先删除所有规格
        $model = new GoodsSpec;
        $isUpdate && $model->removeAll($this['goods_id']);
        // 添加规格数据
        if ($data['spec_type'] == '10') {
            // 单规格
            $this->spec()->save($data['spec']);
        } else if ($data['spec_type'] == '20') {
            // 添加菜品与规格关系记录
            $model->addGoodsSpecRel($this['goods_id'], $data['spec_many']['spec_attr']);
            // 添加菜品sku
            $model->addSkuList($this['goods_id'], $data['spec_many']['spec_list']);
        }
    }

    /**
     * 删除
     */
    public function remove()
    {
        // 开启事务处理
        Db::startTrans();
        try {
            // 删除菜品sku
            (new GoodsSpec)->removeAll($this['goods_id']);
            // 删除菜品图片
            $this->image()->delete();
            // 删除当前菜品
            $this->delete();
            // 事务提交
            Db::commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            Db::rollback();
            return false;
        }
    }

}
